<?php

/**
 *
 * Klasa validatora
 *
 */
class Validator extends UserValidator {

    /**
     * lista bledow
     *
     * @var array
     */
    private $errors = array();

    /**
     * lista validatorow
     *
     * @var array
     */
    private $validators = array();

    /**
     * validowane dane
     *
     * @var array
     */
    private $values = array();

    /**
     * przechowuje dane do generowania validatorów serverowych
     * 
     * @var unknown_type
     */
    private $js_values = array();

    /**
     * konstruktor klasy
     * oczekuje danych w postaci array('nazwa_zmiennej'=>$wartosc, ...)
     *
     * @param array $data
     */
    public function __construct($data) {
        $this->ResetErrorList();

        $data = $this->Remover($data);

        $this->values = $data;
    }

    public function Remover($data) {

        if (!is_array($data)) {
            $data = rawurldecode($data);
            //            $data = Utils::url2pl($data);
        } else {
            $output = array();
            foreach ($data as $key => $val) {
                $output[$key] = self::Remover($val);
            }
            $data = $output;
        }

        return $data;
    }

    /**
     * Dodaje parametry dla validatora
     *
     * @param string $field
     * @param string $match
     * @param string $msg
     * @param array $data
     */
    public function AddValidator($field, $match, $msg, $data = null) {

        $this->validators[] = array('field' => $field, 'match' => $match, 'msg' => $msg, 'data' => $data);
    }

    /**
     * Resetuje liste bledow
     *
     */
    public function ResetErrorList() {
        $this->errors = array();
    }

    /**
     * dodaje dowolny blad do dowolnego pola
     *
     * @param string $field
     * @param string $msg
     */
    public function AddError($field, $msg, $value = '') {
        $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
    }

    /**
     * Zwraca zadana wartosc
     *
     * @param string|array $field
     * @return string
     */
    private function GetValue($field) {
        if (is_array($field)) {
            if (isset($this->values[$field['name']][$field['key']])) {
                return $this->values[$field['name']][$field['key']];
            }
        } else {
            if (isset($this->values[$field])) {
                return $this->values[$field];
            }
        }
    }

    public function IsBadLanguage($field, $msg, $alterField = null) {
        $value = $this->GetValue($field);
        if (RestrictedKeywordDAL::CheckBadLanguage($value)) {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * sprawdza czy niepusty
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsEmpty($field, $msg, $alternativeField = null) {
        $this->js_values[$field][] = array(
            'restriction' => 'required',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => ''
        );



        $value = trim($this->GetValue($field));

        if (empty($value)) {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', '');
            }
            return false;
        } else {
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', html_entity_decode($this->GetValue($field), ENT_NOQUOTES, 'UTF-8'));
            }
            return true;
        }
    }

    public function IsEmptyArray($field, $msg, $alternativeField = null) {
        $this->js_values[$field][] = array(
            'restriction' => 'emptyArray',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => 'return $("input.preference:[checked=true]").length>0?true:false;'
        );

        $field = 'preference';

        $value = $this->GetValue($field);
        if (empty($value)) {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', '');
            }
            return false;
        } else {
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', html_entity_decode($this->GetValue($field), ENT_NOQUOTES, 'UTF-8'));
            }
            return true;
        }
    }

    /**
     * sprawdza czy niedłuższy niż
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsLonger($field, $length, $msg, $alternativeField = null) {
        $this->js_values[$field][] = array(
            'restriction' => 'maxlength',
            'value' => $length,
            'message' => $msg,
            'custom_script' => ''
        );



        $value = trim($this->GetValue($field));
        if (strlen($value) > $length) {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * sprawdza czy niekrótszy niż
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsShorter($field, $length, $msg, $alternativeField = null) {
        $this->js_values[$field][] = array(
            'restriction' => 'minlength',
            'value' => $length,
            'message' => $msg,
            'custom_script' => ''
        );



        $value = trim($this->GetValue($field));
        if (strlen($value) < $length) {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * musi byc pusty
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function OnlyEmpty($field, $msg, $alternativeField = null) {
        $value = $this->GetValue($field);
        if (trim($value) == "") {
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', html_entity_decode($this->GetValue($field), ENT_NOQUOTES, 'UTF-8'));
            }
            return false;
        } else {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            if ($field == 'city') {
                SessionProxy::SetValue('_city_limit_', '');
            }
            return true;
        }
    }

    public function IsValidOnline($field, $msg, $url) {
        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => "'$url'",
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        $out = file_get_contents($url . '?' . urlencode($field) . '=' . urlencode($value));
        if ($out == 'true') {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdzenie czy wartosc istnieje w bazie
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function IsInDatabase($field, $msg, $classDAL, $db_field_name, $js_validation_url = '') {
        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => "'$js_validation_url'",
            'message' => $msg,
            'custom_script' => ''
        );


        $value = $this->GetValue($field);
        $res = null;
        if ($field == 'login') {
            $res = MfUserDAL::CheckGlobalUserNikckname($value);
        } elseif ($field == 'email') {
            $res = MfUserDAL::CheckGlobalUserEmail($value);
        }

        if (!is_null($res)) {
            if ($res) {
                return true;
            } else {
                $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
                return false;
            }
        }


        eval('$dalObj = ' . $classDAL . 'DAL::getDalDataObj();');
        $dalObj->setCondition(array($db_field_name => $value));
        $dalObj->setCount(true);
        eval('$result = ' . $classDAL . 'DAL::GetResult($dalObj);');
        if ($result > 0) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdzenie czy wartosc istnieje w bazie
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function NotInDatabase($field, $msg, $classDAL, $db_field_name) {
        $value = $this->GetValue($field);
        eval('$result = ' . $classDAL . 'DAL::GetResult(array($db_field_name => \'"' . $value . '"\'), array(), null, null, true);');
        if ($result == 0) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return true;
        } else {
            return false;
        }
    }

    /**
     * Metoda sprawdza czy dla danego wpisu isnieją powiązania zadanego typu w tabeli MfLink
     *
     * @param string $field - pole formularza
     * @param string $alternativeField - alternatywne pole
     * @param integer $srcId - id wpisu (artykuł, rapoty itp.)
     * @param string $srcType
     * @param string $destinationType
     * @param string $msg
     * @param string $classDAL
     * @return bool
     */
    public function IsInMfLink($field, $alternativeField = null, $srcId, $srcType, $destinationType, $msg) {

        $value = $this->GetValue($field);
        //$fields = array(
        //  'mf_link.id_source' => $srcId,
        // 'mf_link.source_type' => '"'.$srcType.'"',
        //  'mf_link.destination_type' => '"'.$destinationType.'"'
        //);
        //$result = MfLinkDAL::GetResult($fields, array(), null, null, true);
        //if($srcId == -1 && is_array($value)) $result = 1;
        if (isset($value) && is_array($value)) {
            $result = 1;
        } else {
            $result = 0;
        }

        if ($result == 0) {
            if (is_null($alternativeField)) {
                $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            } else {
                $this->errors[] = array("field" => $alternativeField, "value" => $value, "msg" => $msg);
            }
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdzenie czy wartosci w bazie nie ma
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function IsntInDatabase($field, $msg, $classDAL, $db_field_name) {
        $value = $this->GetValue($field);
        eval('$result = ' . $classDAL . 'DAL::GetResult(array($db_field_name => $value), array(), null, null, true);');
        if ($result > 0) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdzenie czy numer PWZ jest w bazie
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function IsNotInDatabase($field, $msg, $db_field_name, $profileId = 0) {
        $value = $this->GetValue($field);
        $result = PhysicianDAL::GetResult(array($db_field_name => addslashes($value), 'id_nm_physician' => array('condition' => '!=', 'value' => $profileId)), array(), null, null, true);
        if ($result == 0) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
        }
    }

    /**
     * sprawdza czy zaznaczony
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsChecked($field, $msg, $fieldId) {
        $value = $this->GetValue($field);
        if (trim($value) != "on") {
            if (is_null($fieldId)) {
                $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            } else {
                $this->errors[] = array("field" => $fieldId, "value" => $value, "msg" => $msg);
            }

            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy string
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsString($field, $msg) {
        $value = $this->GetValue($field);
        if (!is_string($value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy liczba
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsNumber($field, $msg) {
		
        $this->js_values[$field][] = array(
            'restriction' => 'number',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => ''
        );		
		
        $value = $this->GetValue($field);
        if (!is_numeric($value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy calkowite
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsInteger($field, $msg) {
        $value = $this->GetValue($field);
        if (!is_integer($value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy zmiennoprzecinkowa
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsFloat($field, $msg) {
		
        $this->js_values[$field][] = array(
            'restriction' => 'regexp',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => '
			    return value.match(' . $regexp . ');
			'
        );		
		
        $value = $this->GetValue($field);
        if (!is_float($value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    public function Regexp($field, $msg, $regexp) {
        $this->js_values[$field][] = array(
            'restriction' => 'regexp',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => '
			    return value.match(' . $regexp . ');
			'
        );
        $value = $this->GetValue($field);

        if (!preg_match($regexp, $value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy literowe
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsAlpha($field, $msg) {
        $value = $this->GetValue($field);
        $pattern = "/^[a-zA-Z]+$/";
        if (preg_match($pattern, $value)) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function isDate($field, $msg, $alternativeField=null) {
        $this->js_values[$field][] = array(
            'restriction' => 'date',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        $pattern = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})";
        if (ereg($pattern, $value))
            return true;
        else {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function isDateCustom($field, $msg, $custom, $alternativeField=null) {
        $this->js_values[$field][] = array(
            'restriction' => 'dateCustom',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => $custom
        );

        $value = $this->GetValue($field);
        $pattern = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})";
        if (ereg($pattern, $value))
            return true;
        else {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function isHour($field, $msg, $alternativeField=null) {
        $value = $this->GetValue($field);
        $pattern = "([0-9]|[0-1][0-9]|[2][0-4]):([0-5][0-9])";
        if (ereg($pattern, $value))
            return true;
        else {
            $this->errors[] = array("field" => (isset($alternativeField) ? $alternativeField : $field), "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function IsPrevDate($field, $msg) {
        $value = $this->GetValue($field);

        $date_prev = strtotime($value);

        if ($date_prev > time()) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

    public function isGoodLogin($field, $msg) {
        $value = $this->GetValue($field);
        //		echo mb_detect_encoding($value);
        //		if( mb_detect_encoding($value) == "UTF-8")
        //$value = iconv("utf-8", "iso-8859-2", $value);

        $len = "{" . strlen($value) . "}";

        $pattern = "^[a-zA-Z0-9_��������桦���Ư�� \+\-]$len$";
        //!@\$%\*\(\)\^\-
        if (strlen($value) >= 3 && strlen($value) <= 15 && ereg($pattern, $value, $matches)) {
            //var_dump($matches);
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function isGoodPassword($field, $msg) {
        $value = $this->GetValue($field);
        if (strlen($value) >= 6)
            return true;
        else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdza czy w przedziale
     *
     * @param string $field
     * @param string $msg
     * @param integer $min
     * @param integer $max
     * @return boolean
     */
    public function IsWithinRange($field, $msg, $min, $max) {
        $value = $this->GetValue($field);

        $min = floatval($min);
        $max = floatval($max);
        $value = floatval($value);

        if ($value >= $min && $value <= $max) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdza czy email
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsEmailAddress($field, $msg) {
        $this->js_values[$field][] = array(
            'restriction' => 'email',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => ''
        );


        $value = $this->GetValue($field);
        $pattern = "/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/";
        if (preg_match($pattern, $value)) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdza czy oba pola sa takie sam
     *
     * @param string $field1
     * @param string $field2
     * @param string $msg
     * @return boolean
     */
    public function IsNotEqual($field1, $field2, $msg) {
        $value1 = $this->GetValue($field1);
        $value2 = $this->GetValue($field2);

        $this->js_values[$field1][] = array(
            'restriction' => 'equalTo',
            'value' => "'#$field2'",
            'message' => $msg,
            'custom_script' => ''
        );

        if ($value1 == $value2) {
            return true;
        } else {
            $this->errors[] = array("field" => $field2, "value" => "", "msg" => $msg);
            return false;
        }
    }

    /**
     * Sprawdza czy oba pola sa takie sam
     *
     * @param string $field1
     * @param string $field2
     * @param string $msg
     * @return boolean
     */
    public function IsEqual($field1, $field2, $msg) {
        $value1 = $this->GetValue($field1);
        $value2 = $this->GetValue($field2);

        if ($value1 != $value2) {
            return true;
        } else {
            $this->errors[] = array("field" => $field2, "value" => "", "msg" => $msg);
            return false;
        }
    }

    public function IsEqualValue($field, $value, $msg) {
        $value1 = $this->GetValue($field);

        if ($value1 != $value) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => "", "msg" => $msg);
            return false;
        }
    }

    public function IsNotEqualValue($field, $value, $msg) {
        $value1 = $this->GetValue($field);

        if ($value1 == $value) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => "", "msg" => $msg);
            return false;
        }
    }

    /**
     * Zwraca liste bledow
     *
     * @return array
     */
    public function GetErrorList() {
        return $this->errors;
    }

    /**
     * Zwraca liczbe bledow
     *
     * @return integer
     */
    public function IsError() {
        if (sizeof($this->errors) > 0) {
            return sizeof($this->errors);
        } else {
            return false;
        }
    }

    public function CheckCaptcha($field, $msg) {

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('goToRegistryCaptchaCheck') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value1 = $this->GetValue($field);
        $value2 = Request::GetCookie(CAPTCHA_COOKIE_NAME, false);

        if (md5(strtolower($value1) . CAPTCHA_SEED) == $value2)
            return true;
        else {
            $this->errors[] = array("field" => 'captcha', "value" => $value1, "msg" => $msg);
            return false;
        }
    }

    public function IsFile($field, $msg) {
        $value = $this->GetValue($field);
        if (is_file($value)) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function IsGoodImageFormat($field, $msg) {
        $value = $this->GetValue($field);
        switch ($value) {
            default:
                $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
                return false;
                break;
            case 'image/pjpeg':
                break;
            case 'image/gif':
                break;
            case 'image/jpeg':
                break;
            case 'image/png':
                break;
            case 'image/x-png':
                break;
        }
    }

    public function IsValidFeed($field, $msg) {
        $value = $this->GetValue($field);
        $value = str_replace('&lt;x&gt;', '', $value);
        if (empty($value)) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
        } else {

            $feed = new FeedReader($value);
            $feed->init();
            $feed->handle_content_type();

            if ($feed->error()) {
                $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            } else {
                return true;
            }
        }
    }

    /**
     * Sprawdzenie czy numer specjalizacja jest w bazie
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function IsInDatabaseSpecialty($field, $msg, $db_field_name) {

        $value = $this->GetValue($field);
        if (is_array($value)
        )
            foreach ($value AS $k => $val) {

                if (strlen($val) == 0) {

                    //            			$this->errors[] = array("field" => 'speciality_'.$k.'', "value" => $val, "msg" => 'To pole nie może być puste');
                    $this->errors[] = array("field" => $field . '[' . $k . ']', "value" => $val, "msg" => 'To pole nie może być puste');
                    //					$this->errors[] = array("field" => $field.'_'.$k, "value" => $val, "msg" => 'To pole nie może być puste');
                } else {

                    $result = SpecialtyDAL::GetResult(array($db_field_name => addslashes($val)), array(), null, null, true);
                    if ($result > 0) {
                        return true;
                    } else {

                        //     	    				$this->errors[] = array("field" => 'speciality_'.$k.'', "value" => $val, "msg" => $msg);
                        $this->errors[] = array("field" => $field . '[' . $k . ']', "value" => $val, "msg" => $msg);
                        //						$this->errors[] = array("field" => $field.'_'.$k, "value" => $val, "msg" => $msg);
                    }
                }
            }
    }

    /**
     * Sprawdzenie czy numer specjalizacja jest w bazie
     *
     * @param <type> $field
     * @param <type> $msg
     */
    public function IsInDatabaseArticleCategory($field, $msg, $id) {
        $val = $this->values['name'];
        $result = MfArticleCategoryDescriptionDAL::GetResult(array('name' => addslashes($val)), array(), null, null, true);
        if ($result > 0 && $val != '') {
            if ($id == -1) {
                $this->errors[] = array("field" => $field, "value" => $val, "msg" => $msg);
                return count($result);
            }
        } else {
            return false;
        }
    }

    public function IsNotProvince($field, $msg, $db_field_name = null) {
        $value = $this->GetValue($field);
        $sql = 'SELECT COUNT(*) AS count FROM nmd_province WHERE name LIKE "' . $value . '"';
        $result = Registry::Get('db')->prepare($sql)->execute()->FetchAllAssoc();
        if (intval($result[0]['count']) == 0) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
        }
    }

    public function IsNotCity($field, $msg, $db_field_name = null) {
        $value = $this->GetValue($field);
        $sql = 'SELECT COUNT(*) AS count FROM nmd_city WHERE name LIKE "' . $value . '"';
        $result = Registry::Get('db')->prepare($sql)->execute()->FetchAllAssoc();
        if (intval($result[0]['count']) == 0) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
        }
    }

    public function IsArray($field, $msg, $fieldId = null) {

        $value = $this->GetValue($field);
        if (is_array($value) && !empty($value)) {
            return true;
        } else {
            if (is_null($fieldId)) {
                $this->errors[] = array("field" => $field . "[0]", "value" => $value, "msg" => $msg);
                $this->errors[] = array("field" => $field . "[1]", "value" => $value, "msg" => $msg);
                $this->errors[] = array("field" => $field . "[2]", "value" => $value, "msg" => $msg);
            } else {
                $this->errors[] = array("field" => $fieldId, "value" => $value, "msg" => $msg);
            }
            return false;
        }
    }

    /**
     * Waliduje dane przy pomocy wprowadzonych validatorow
     *
     */
    public function Validate() {
        foreach ($this->validators as $validator) {
            if (isset($validator['field']) && isset($validator['match']) && isset($validator['msg'])) {
                if ($validator['match'] == 'empty') {
                    $this->IsEmpty($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'string') {
                    $this->IsString($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'number') {
                    $this->IsNumber($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'integer') {
                    $this->IsInteger($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'float') {
                    $this->IsFloat($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'alpha') {
                    $this->IsAlpha($validator['field'], $validator['msg']);
                } else if ($validator['match'] == 'range') {
                    $this->IsWithinRange($validator['field'], $validator['msg'], $validator['data']['min'], $validator['data']['max']);
                } else if ($validator['match'] == 'email') {
                    $this->IsEmail($validator['field'], $validator['msg']);
                }
            }
        }
    }

    public function CheckDimensions($file, $width, $height) {
        //Utils::ArrayDisplay($file);
        $dimensions = getimagesize($file['tmp_name']);
        $img_width = $dimensions[0];
        $img_height = $dimensions[1];
        if ($img_width != $width || $img_height != $height) {
            $this->errors[] = array("field" => $field, "value" => $val, "msg" => $msg);
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sprawdza czy email
     *
     * @param string $field
     * @param string $msg
     * @return boolean
     */
    public function IsPostalCode($field, $msg) {
        $this->js_values[$field][] = array(
            'restriction' => 'postal',
            'value' => 'true',
            'message' => $msg,
            'custom_script' => '
			    return value.match(/^\\d\\d-\\d\\d\\d$/);
			'
        );


        $value = $this->GetValue($field);
        $pattern = "/^[0-9]{2}[-][0-9]{3}$/";
        if (preg_match($pattern, $value)) {
            return true;
        } else {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
    }

    public function IsFutureDate($field, $msg) {
        $value = $this->GetValue($field);

        $date_prev = strtotime($value);

        if ($date_prev + 24 * 60 * 50 <= time()) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

    public function getJsValues() {
        return $this->js_values;
    }

    public function CheckNewPerformerNickname($field, $current, $msg) {
        //performerAjaxCheckNewNickname

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('performerAjaxCheckNewNickname') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        if ($current == $value) {
            return true;
        }

//	    $count = PerformerDAL::GetResult(PerformerDAL::GetDalDataObj(array(
//		'condition' => array(
//		    'login' => $value
//		),
//		'count' => true
//	    )));

        $res = MfUserDAL::CheckGlobalUserNikckname($value);
        if (!$res) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

    public function CheckNewAgencyNickname($field, $current, $msg) {
        //performerAjaxCheckNewNickname

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('agencyAjaxCheckNewNickname') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        if ($current == $value) {
            return true;
        }

//	    $count = AgencyDAL::GetResult(AgencyDAL::GetDalDataObj(array(
//		'condition' => array(
//		    'login' => $value
//		),
//		'count' => true
//	    )));

        $res = MfUserDAL::CheckGlobalUserNikckname($value);
        if (!$res) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

    public function CheckNewUserNickname($field, $current, $msg) {
        //performerAjaxCheckNewNickname

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('userAjaxCheckNewNickname') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        if ($current == $value) {
            return true;
        }

//	    $count = UserDAL::GetResult(UserDAL::GetDalDataObj(array(
//		'condition' => array(
//		    'login' => $value
//		),
//		'count' => true
//	    )));
        $res = MfUserDAL::CheckGlobalUserNikckname($value);
        if (!$res) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

    public function CheckNewUserEmail($field, $current, $msg) {
        //performerAjaxCheckNewNickname

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('userAjaxCheckNewEmail') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        if ($current == $value) {
            return true;
        }

//	    $count = UserDAL::GetResult(UserDAL::GetDalDataObj(array(
//		'condition' => array(
//		    'login' => $value
//		),
//		'count' => true
//	    )));
        $res = MfUserDAL::CheckGlobalUserEmail($value);
        if (!$res) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }
    
    // by WOWI
    public function notEqual($field1, $value2, $msg) {
        $value1 = $this->GetValue($field1);
        $this->js_values[$field1][] = array(
            'restriction' => 'notEqual',
            'value' => "'$value2'",
             'message' => $msg,
            'custom_script' => ''
        );
        if ($value1 == $value2) {
             return true;
        } else {
            $this->errors[] = array("field" => $field1, "value" => "", "msg" => $msg);
            return false;
        }
        
    }


    public function isAgency($field, $msg) {
        //performerAjaxCheckNewNickname

        $this->js_values[$field][] = array(
            'restriction' => 'remote',
            'value' => '\'' . Router::GenerateUrl('performerAjaxAgencyValidation') . '\'',
            'message' => $msg,
            'custom_script' => ''
        );

        $value = $this->GetValue($field);
        if ($current == $value) {
            return true;
        }

        $count = AgencyDAL::GetResult(AgencyDAL::GetDalDataObj(array(
                            'condition' => array(
                                'login' => $value
                            ),
                            'count' => true
                        )));

        if ($count == 0) {
            $this->errors[] = array("field" => $field, "value" => $value, "msg" => $msg);
            return false;
        }
        else
            return true;
    }

}

